<?php

namespace app\api\controller;

use app\admin\model\FwxxCategory;
use app\admin\model\FwxxComment;
use app\common\controller\Api;
use think\Db;

/**
 * 房屋信息接口
 */
class Fwxx extends Common
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    protected $fwxxModel = null;
    protected $fwxxCategoryModel = null;
    protected $fwxxCommentModel = null;


    public function _initialize()
    {
        parent::_initialize();
        $this->fwxxModel = new \app\admin\model\Fwxx();
        $this->fwxxCategoryModel =new FwxxCategory();
        $this->fwxxCommentModel =new FwxxComment();
    }

    /**
     * 房屋信息分类列表
     *
     */
    public function getfwxxCategoryList()
    {
        $fwxxCategory = $this->fwxxCategoryModel
            ->select();
        $this->outputData($fwxxCategory);
    }
    /**
     * 房屋信息列表
     */
    public function getfwxxList()
    {
        $page = $this->request->get('page') - 1;
        $page=$page>0?$page:0;
        $limit = $this->request->get('pageSize');
        $limit=$limit>0?$limit:5;
        $start = $page * $limit;
        $status=$this->request->get('status');
        $catid=$this->request->get('catid');
        $dengji=$this->request->get('dengji');
        $loudong=$this->request->get('loudong');
        $groupId=$this->request->get('group_id');
        $uid=$this->request->get('uid');
        $fwbm=$this->request->get('fwbm');

        $where=[];
        //按楼栋编码
        if($fwbm){
            $ldxx=Db('ldxx')->where('ldbh',$fwbm)->value('id');
            $where['f.loudong']=$ldxx;
        }
        //按状态
        if($status){
            $where['f.status']=$status;
        }
        //按分类
        if($catid){
            $where['f.atid']=$catid;
        }
        //按等级
        if(isset($dengji)){
            $where['f.dengji']=$dengji;
        }
        //按楼栋
        if(isset($loudong)){
            $where['f.loudong']=$loudong;
        }
        //按用户组
        if(isset($groupId)){
            $where['f.groupid']=$groupId;
        }
        //按用户
        if(isset($uid)){
            $where['f.uid']=$uid;
        }
        $fwxxList = $this->fwxxModel
            ->alias('f')
            ->join('FwxxCategory fc','f.catid=fc.id')
            ->where($where)
            ->field('f.*,fc.name')
            ->limit($start,$limit)
            ->select();
        if($fwxxList) {
            foreach ($fwxxList as &$value) {
                $value['level'] = str_replace([0, 1, 2], ['宽管级', '关注级', '严管级'], $value['dengji']);
                //图片
                $image = explode(',', $value['image']);
                $imageArr = [];
                if (is_array($image)) {
                    foreach ($image as $ivalue) {
                        $imageArr[] = $ivalue;
                    }
                }
                $value['image'] = $imageArr;
                //隐患条数
                $value['toale'] = Db('fxyh')->where('fangwuxinxi', $value['id'])->count();
                //配套
                // $peitao = explode(',', $value['peitao']);
                // $peitaoArr=[];
                //if (is_array($peitao)) {
                //     foreach ($peitao as $pvalue) {
                //        $peitaoArr[]= Db('peitao')->where('id', $pvalue)->value('name');
                //    }
                //}
                //$value['peitao'] = $peitaoArr;
                //装修
                $zhuangxiu = explode(',', $value['zhuangxiu']);
                $zhuangxiuArr = [];
                if (is_array($zhuangxiu)) {
                    foreach ($zhuangxiu as $zvalue) {
                        $zhuangxiuArr[] = Db('zhuangxiu')->where('id', $zvalue)->value('name');
                    }
                }
                $value['zhuangxiu'] = $zhuangxiuArr;
                //fxjg
                $fxjg = explode(',', $value['fxjg']);
                $fxjgArr = [];
                if (is_array($fxjg)) {
                    foreach ($fxjg as $fvalue) {
                        $fxjgArr[] = Db('fxjg')->where('id', $fvalue)->value('name');
                    }
                }
                $value['fxjg'] = $fxjgArr;
                //家具
                $jiaju = explode(',', $value['jiaju']);
                $jiajuArr = [];
                if (is_array($jiaju)) {
                    foreach ($jiaju as $jvalue) {
                        $jiajuArr[] = Db('jiaju')->where('id', $jvalue)->value('name');
                    }
                }
                $value['jiaju'] = $jiajuArr;
                //平面图
                $img = explode(',', $value['img']);
                $imgArr = [];
                if (is_array($img)) {
                    foreach ($img as $imvalue) {
                        $imgArr[] = $imvalue;
                    }
                }
                $value['img'] = $imgArr;
            }
        }
        $this->outputData($fwxxList);
    }
    /**
     * 附近房屋列表
     */
    public function getNearFwxx(){
        $lat=$this->request->get('lat');
        $lng=$this->request->get('lng');
        $sql=$this->myNearSql($lng,$lat,'dr_ldxx',100);
        $list=Db::query($sql);
        if(!count($list)){
            $this->outputData($list);
        }
        foreach ($list as &$value){
            $value['level']=str_replace([0,1,2],['宽管级','关注级','严管级'],$value['dengji']);
            $value['yznum']=Db('fwxx')->where(['loudong'=>$value['id'],'syqk'=>'已租'])->count();
            $value['dznum']=Db('fwxx')->where(['loudong'=>$value['id'],'syqk'=>'待租'])->count();

        }
        $this->outputData($list);
    }
    /**
     * 投诉房屋列表
     */
    public function yhFwList(){
        $uid=$this->request->get('uid');
        //获取隐患列表
        $yhList=Db('fxyh')->where(['uid'=>$uid])->column('fangwuxinxi');
        $yhfwlist=[];
        if($yhList) {
            $yhfwlist=$this->fwxxModel->where(['id'=>['in',$yhList]])->select();
        }
        $fwList=$this->fwxxModel->where(['uid'=>$uid])->select();
        $list=array_merge($yhfwlist,$fwList);
        $this->outputData($list);
    }
    /**
     * 房屋信息
     */
    public function getfwxxDetail()
    {
        $id=$this->request->get('id');
        $fwbh=$this->request->get('fwbh');
        $where=[];
        if(is_numeric($id)){
           $where['f.id']=$id;
        }else {
            if ($fwbh) {
                $where['f.fwbh'] = $fwbh;
            }
        }
        $fwxxList = Db('fwxx')
            ->alias('f')
            ->join('FwxxCategory fc','f.catid=fc.id')
            ->field('fc.name,f.*')
            ->where($where)
            ->find();
        if($fwxxList) {
            $fwxxList['level'] = str_replace([0, 1, 2], ['宽管级', '关注级', '严管级'], $fwxxList['dengji']);
            //图片
            $image = explode(',', $fwxxList['image']);
            $imageArr=[];
            if (is_array($image)) {
                foreach ($image as $ivalue) {
                    $imageArr[]= $ivalue;
                }
            }
            $fwxxList['image'] = $imageArr;
            //隐患条数
            $fwxxList['toale']=Db('fxyh')->where('fangwuxinxi',$fwxxList['id'])->count();
            //配套
           // $peitao = explode(',', $fwxxList['peitao']);
           // $peitaoArr=[];
            //if (is_array($peitao)) {
           //     foreach ($peitao as $pvalue) {
            //        $peitaoArr[]= Db('peitao')->where('id', $pvalue)->value('name');
            //    }
            //}
            //$fwxxList['peitao'] = $peitaoArr;
            //装修
            $zhuangxiu = explode(',', $fwxxList['zhuangxiu']);
            $zhuangxiuArr = [];
            if (is_array($zhuangxiu)) {
                foreach ($zhuangxiu as $zvalue) {
                    $zhuangxiuArr[]= Db('zhuangxiu')->where('id', $zvalue)->value('name');
                }
            }
            $fwxxList['zhuangxiu'] = $zhuangxiuArr;
            //fxjg
            $fxjg = explode(',', $fwxxList['fxjg']);
            $fxjgArr =[];
            if (is_array($fxjg)) {
                foreach ($fxjg as $fvalue) {
                    $fxjgArr[]= Db('fxjg')->where('id', $fvalue)->value('name');
                }
            }
            $fwxxList['fxjg'] = $fxjgArr;
            //家具
            $jiaju = explode(',', $fwxxList['jiaju']);
            $jiajuArr = [];
            if (is_array($jiaju)) {
                foreach ($jiaju as $jvalue) {
                    $jiajuArr[]= Db('jiaju')->where('id', $jvalue)->value('name');
                }
            }
            $fwxxList['jiaju'] = $jiajuArr;
            //平面图
            $img = explode(',', $fwxxList['img']);
            $imgArr = [];
            if (is_array($img)) {
                foreach ($img as $imvalue) {
                    $imgArr[]= $imvalue;
                }
            }
            $fwxxList['img'] = $imgArr;
        }
        $this->outputData($fwxxList);
    }
    /**
     * 房屋信息关系
     */
    public function getfwxxUpadte()
    {
        $id = $this->request->request('id');
        $fxjg = $this->request->request('fxjg');
        $zhuangxiu = $this->request->request('zhuangxiu');
        $mianji = $this->request->request('mianji');
        $louceng = $this->request->request('louceng');
        $peitao = $this->request->request('peitao');
        $zujin = $this->request->request('zujin');
        $image = $this->request->request('image');
        $img = $this->request->request('description');
        $fwbh=$this->request->request('fwbh');
        if (is_array($image)) {
            $image = join(',', $image);
        }
        if (is_array($image)) {
            $img = join(',', $img);
        }
        if (!is_numeric($id)) {
            $this->outputError('参数错误');
        }
        //装修
        $zhuangxius= Db('zhuangxiu')->where('name', $zhuangxiu)->find();
        $zhuangxiut=$zhuangxius['id'];
        //户型结构
        $fxjgs= Db('fxjg')->where('name', $fxjg)->find();
        $fxjgt=$fxjgs['id'];

        $re = $this->fwxxModel
            ->where('id', $id)
            ->update(['fxjg' => $fxjgt, 'zhuangxiu' => $zhuangxiut, 'mianji' => $mianji, 'louceng' => $louceng, 'peitao' => $peitao, 'zujin' => $zujin, 'image' => $image, 'img' => $img,'fwbh'=>$fwbh]);
        if ($re) {
            $this->outputData('更新成功');
        } else {
            $this->outputData('更新失败');
        }
    }
    /**
     * 房屋评论列表
     */
    public function getFwxxCommentList()
    {
        $cid=$this->request->request('cid');
        $cuid=$this->request->request('uid');
        $where=[];
        if($cid){
            $where['c.cid']=$cid;
        }

        if($cuid){
            $where['c.uid']=$cuid;
        }
        $CommentList = $this->fwxxCommentModel
            ->alias('c')
            ->join('fwxx ','fwxx.id=c.cid')
            ->field('fwxx.title,c.*')
            ->where($where)
            ->select();
        foreach ($CommentList as &$value){
            $value['inputtime']=$this->getTime($value['inputtime']);
        }
        $this->outputData($CommentList);
    }
    /**
     * 房屋信息
     */
    public function getFwxxCommentDetail()
    {
        $id=$this->request->get('id');
        if(!is_numeric($id)){
            $this->outputError('参数错误');
        }
        $fwxxList = $this->fwxxCommentModel
            ->alias('c')
            ->join('fwxx ','fwxx.id=c.cid')
            ->field('fwxx.title,c.*')
            ->where('c.id',$id)
            ->find();
        $fwxxList['inputtime']=$this->getTime($fwxxList['inputtime']);
        $this->outputData($fwxxList);
    }
    /**
     *添加品论
     */
    public function getfwxxCommentAdd()
    {
        $uid=$this->request->request('uid');
        $cid=$this->request->request('cid');
        $author=$this->request->request('author');
        $content=$this->request->request('content');
        $dianhuahaoma=$this->request->request('dianhuahaoma');
        if(!$cid||!$uid||!$author||!$content||!$dianhuahaoma){
            $this->outputError('请填写完整信息');
        }
        $re = $this->fwxxCommentModel
            ->insert(['uid'=>$uid,'cid'=>$cid,'author'=>$author,'content'=>$content,'dianhuahaoma'=>$dianhuahaoma,'inputtime'=>time()]);
        if($re){
            $this->outputData('评论成功');
        }else{
            $this->outputData('评论失败');
        }
    }
    /**
     * 房屋评论删除
     */
    public function getFwxxCommentDelete()
    {
        $id=$this->request->request('id');
        $uid=$this->request->request('uid');
        if(!is_numeric($id)){
            $this->outputError('参数错误');
        }
        $res = $this->fwxxCommentModel
        	->where('id', $id)
        	->delete();
        if($res){
            $this->outputData('删除成功');
        }else{
            $this->outputData('删除失败');
        }

    }
}
